﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TomNet.Data.Entity;
using TomNet.IMSC.Core.Contracts.Common;
using TomNet.IMSC.Model.DbModels.Common;
using TomNet.IMSC.Model.DbModels.Decoration;
using TomNet.IMSC.Web.Areas.Decoration.Models;
using TomNet.IMSC.Web.Common;
using TomNet.IMSC.Web.Filters;
using TomNet.Web.Mvc;
using TomNet.Web.Mvc.Security;
using TomNet.Web.Mvc.UI;

namespace TomNet.IMSC.Web.Areas.Decoration.Controllers
{
    [Description("装修-统计")]
    [GlobalAuthorization]
    public class DecorationStatisticsController : BaseController
    {

        DefaultDbContext DbContext = new DefaultDbContext();

        #region 装修公司统计
        [Description("装修公司统计")]
        public ActionResult DecorationCompanyStatistics()
        {
            return View();
        }
        [Description("装修公司统计")]
        [HttpPost, AjaxOnly]
        public ActionResult DecorationCompanyStatisticsAsync(int pageIndex, int pageSize, DateTime beginDate, DateTime endDate)
        {
            var cookies = Common.CookiesManagement.GetCookies();
            var ticket = Common.CookiesManagement.GetTicket(cookies);
            //获取权限
            //LoginModel login = CookiesManagement.GetLoginModel(CookiesManagement.GetTicket());            
            var loginModel = CookiesManagement.GetLoginModel(ticket);

            var query = DbContext.SqlQuery<DecorationCompanyModel>("select * from [dbo].[Fn_Table_Report_Decoration4DecorationCompany](@p0,@p1,@p2)", loginModel.Id, beginDate, beginDate);
            var total = query.Count();

            //默认给一个排序的字段
            query = query.OrderByDescending(m => m.DecorationCompanyID);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : Enumerable.Empty<DecorationCompanyModel>().AsQueryable();

            //此处可以采用匿名对象 GridData<object>
            GridData<DecorationCompanyModel> gridData = new GridData<DecorationCompanyModel>() { Total = total, Rows = query.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm:ss");
        }
        #endregion

        #region 中介统计
        [Description("中介统计")]
        public ActionResult DecorationIntermediaryStatistics()
        {
            return View();
        }
        [Description("中介统计")]
        [HttpPost, AjaxOnly]
        public ActionResult DecorationIntermediaryStatisticsAsync(int pageIndex, int pageSize, DateTime beginDate, DateTime endDate)
        {
            var cookies = Common.CookiesManagement.GetCookies();
            var ticket = Common.CookiesManagement.GetTicket(cookies);
            //获取权限
            //LoginModel login = CookiesManagement.GetLoginModel(CookiesManagement.GetTicket());            
            var loginModel = CookiesManagement.GetLoginModel(ticket);

            var query = DbContext.SqlQuery<DecorationIntermediary>("select * from [dbo].[Fn_Table_Report_Decoration4Intermediaries](@p0,@p1,@p2)", loginModel.Id, beginDate, beginDate);

            var total = query.Count();

            //默认给一个排序的字段
            query = query.OrderByDescending(m => m.IntermediaryID);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : Enumerable.Empty<DecorationIntermediary>().AsQueryable();

            //此处可以采用匿名对象 GridData<object>
            GridData<DecorationIntermediary> gridData = new GridData<DecorationIntermediary>() { Total = total, Rows = query.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm:ss");
        }
        #endregion

        #region 渠道统计
        [Description("渠道统计")]
        public ActionResult DecorationOriginyStatistics()
        {
            return View();
        }
        [Description("渠道统计")]
        [HttpPost, AjaxOnly]
        public ActionResult DecorationOriginStatisticsAsync(int pageIndex, int pageSize, DateTime beginDate, DateTime endDate)
        {
            var cookies = Common.CookiesManagement.GetCookies();
            var ticket = Common.CookiesManagement.GetTicket(cookies);
            //获取权限
            //LoginModel login = CookiesManagement.GetLoginModel(CookiesManagement.GetTicket());            
            var loginModel = CookiesManagement.GetLoginModel(ticket);

            var query = DbContext.SqlQuery<DecorationOrigin>("select * from [dbo].[Fn_Table_Report_Decoration4OriginStaff](@p0,@p1,@p2)", loginModel.Id, beginDate, beginDate);

            var total = query.Count();

            //默认给一个排序的字段
            query = query.OrderByDescending(m => m.OriginStaffID);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : Enumerable.Empty<DecorationOrigin>().AsQueryable();

            //此处可以采用匿名对象 GridData<object>
            GridData<DecorationOrigin> gridData = new GridData<DecorationOrigin>() { Total = total, Rows = query.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm:ss");
        }
        #endregion

        #region 销售统计
        [Description("销售统计")]
        public ActionResult DecorationSellStatistics()
        {
            return View();
        }
        [Description("销售统计")]
        [HttpPost, AjaxOnly]
        public ActionResult DecorationSellStatisticsAsync(int pageIndex, int pageSize, DateTime beginDate, DateTime endDate)
        {
            var cookies = Common.CookiesManagement.GetCookies();
            var ticket = Common.CookiesManagement.GetTicket(cookies);
            //获取权限
            //LoginModel login = CookiesManagement.GetLoginModel(CookiesManagement.GetTicket());            
            var loginModel = CookiesManagement.GetLoginModel(ticket);

            var query = DbContext.SqlQuery<DecorationSell>("select * from [dbo].[Fn_Table_Report_Decoration4SellStaff](@p0,@p1,@p2)", loginModel.Id, beginDate, beginDate);

            var total = query.Count();

            //默认给一个排序的字段
            query = query.OrderByDescending(m => m.SellStaffID);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : Enumerable.Empty<DecorationSell>().AsQueryable();

            //此处可以采用匿名对象 GridData<object>
            GridData<DecorationSell> gridData = new GridData<DecorationSell>() { Total = total, Rows = query.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm:ss");
        }
        #endregion

        #region 统计
        [Description("统计")]
        public ActionResult DecorationTotalStatistics()
        {
            return View();
        }
        [Description("统计")]
        [HttpPost, AjaxOnly]
        public ActionResult DecorationTotalStatisticsAsync(int pageIndex, int pageSize, DateTime beginDate, DateTime endDate)
        {
            var cookies = Common.CookiesManagement.GetCookies();
            var ticket = Common.CookiesManagement.GetTicket(cookies);
            //获取权限
            //LoginModel login = CookiesManagement.GetLoginModel(CookiesManagement.GetTicket());            
            var loginModel = CookiesManagement.GetLoginModel(ticket);

            var query = DbContext.SqlQuery<DecorationTotal>("select * from [dbo].[Fn_Table_Report_Decoration4Total](@p0,@p1,@p2)", loginModel.Id, beginDate, beginDate);

            var total = query.Count();

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : Enumerable.Empty<DecorationTotal>().AsQueryable();

            //此处可以采用匿名对象 GridData<object>
            GridData<DecorationTotal> gridData = new GridData<DecorationTotal>() { Total = total, Rows = query.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm:ss");
        } 
        #endregion
    }
}